package com.ctgu.service.impl;


import com.ctgu.common.ResponseResult;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Objects;

@Component
public class ConsumerImpl {
    @Autowired
    private MailServiceImpl mailService;

    /**
     * value: @Queue 注解，用于声明队列，value 为 queueName, durable 表示队列是否持久化, autoDelete 表示没有消费者之后队列是否自动删除
     * exchange: @Exchange 注解，用于声明 exchange， type 指定消息投递策略，我们这里用的 topic 方式
     * key: 在 topic 方式下，这个就是我们熟知的 routingKey
     * @param
     */
    @RabbitListener(bindings =
        @QueueBinding(
            value = @Queue, //临时队列
            exchange = @Exchange(name = "ctguMailExchange",type = "topic"), //指定交换机名称与类型
            key = {"user.*"}
    )
    )
    public void sendEmail(String email){
        String status= String.valueOf(email.charAt(email.length()-1));
        email=email.substring(0,email.length()-1);
        String subject="一码平川跨组织人才管理系统";
        String a ="╭──────────────────────────────────────╮\n" +
                    "        恭喜加入该系统，您的账号已启用。  "+"\n" +
                  "╰──────────────────────────────────────╯";

        String b="╭──────────────────────────────────────╮\n" +
                    "        您的公司涉嫌违规，账户已被停用。  "+"\n" +
                 "╰──────────────────────────────────────╯";
        //发送邮件
        if(status.equals("1")){
            a= LocalDateTime.now()+"\n"+a;
            mailService.sendSimpleMail(email, subject, a);
        }
        else{
            b= LocalDateTime.now()+"\n"+b;
            mailService.sendSimpleMail(email, subject, b);
        }
        //发送邮件
        System.out.println("已发送邮件");
    }

//    @RabbitListener(bindings =
//    @QueueBinding(
//            value = @Queue(value = "ctguMailQueues", durable = "false", autoDelete = "true"),
//            exchange = @Exchange(name = "ctguMailExchange",type = "topic"),
//            key = "user.*"
//    )
//    )
//    public void listUser(String json){
//        //System.out.println(json);
//        User user = JSON.parseObject(json,User.class);
//        userService.save(user);
//    }
}
